package com.framework.modules.sys.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.sys.YhbSysRoleMenuDao;
import com.framework.modules.sys.service.YhbSysRoleMenuService;
import com.framework.yhbEntity.sys.YhbSysRoleMenu;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("yhbSysRoleMenuService")
public class YhbSysRoleMenuServiceImpl  extends ServiceImpl<YhbSysRoleMenuDao,YhbSysRoleMenu> implements YhbSysRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        // 先删除角色与菜单关系
        deleteBatch(new Long[] { roleId });

        if (menuIdList.size() == 0) {
            return;
        }

        // 保存角色与菜单关系
        List<YhbSysRoleMenu> list = new ArrayList<>(menuIdList.size());
        for (Long menuId : menuIdList) {
            YhbSysRoleMenu sysRoleMenuEntity = new YhbSysRoleMenu();
            sysRoleMenuEntity.setYhbSysMenuId(menuId);
            sysRoleMenuEntity.setYhbRoleId(roleId);

            list.add(sysRoleMenuEntity);
        }
        this.insertBatch(list);
    }

    @Override
    public List<Long> queryMenuIdList(Long roleId) {
        return baseMapper.queryMenuIdList(roleId);
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }



}
